<template>
    <div>
        <el-dialog title="目录设置" :visible.sync="visible">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openEdit" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="false"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="180">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button>
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
        </el-dialog>
        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? ( findFormData[pk] ? '编辑' : '添加' ) : '详情'" :visible="editOpen" :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"
                      :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="onBeforeClose" />
        </el-dialog>
    </div>
</template>
<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'

export default {
    name: "SettingContents",
    mixins: [curdMixin],
    components: {EditForm},
    data() {
        return {
            visible: false,
            postData: {},
            tableIsJoinLoad: false,
            // table结构
            columns: [
                {
                    visible: true,
                    label: '编号',
                    prop: 'id',
                    width: 50
                },
                {
                    visible: true,
                    label: '集数',
                    prop: 'chapter_no',
                },
                {
                    visible: true,
                    label: '目录标题',
                    prop: 'title',
                },
                {
                    visible: true,
                    label: '目录内容',
                    prop: 'chapter_val',
                    component:'QuickAdminPopover',
                },
                {
                    visible: true,
                    label: '目录介绍',
                    prop: 'content',
                },
                {
                    visible: true,
                    label: '价格',
                    prop: 'money',
                },
                {
                    visible: true,
                    label: '状态',
                    prop: 'status',
                    component:'QuickAdminTextColor',
                    formatter: (prop, row) => {
                        let type = ''
                        switch (prop) {
                            case 1:
                                type = 'success'
                                break
                            case 2:
                                type = 'warning'
                                break
                            case 3:
                                type = 'info'
                                break

                        }
                        return {
                            value: prop,
                            type: type,
                            text: row.status_text,
                        }
                    },
                },
                {
                    visible: true,
                    label: '学习次数',
                    prop: 'learner_num',
                },
                {
                    visible: true,
                    label: '创建时间',
                    prop: 'create_time',
                },
                {
                    visible: true,
                    label: '更新时间',
                    prop: 'update_time',
                },
            ],
            // 接口地址
            api: {
                index: `/admin/train.post_contents/index`,
                find: `/admin/train.post_contents/find`,
                add: `/admin/train.post_contents/add`,
                edit: `/admin/train.post_contents/edit`,
                delete: `/admin/train.post_contents/delete`,
                export: `/admin/train.post_contents/export`,
                status: `/admin/train.post_contents/status`
            },
            // 查询参数
            queryParams: {
                post_id: { value:undefined, op: '='},
            },
            editFormParams: {},
        }
    },
    computed: {
        maxChapterNo() {
            let num = 0;
            this.tableData.forEach(el=>{
                if (el.chapter_no > num)
                    num = el.chapter_no
            })
            return num
        }
    },
    methods: {
        show(row) {
            this.visible = true
            this.postData = row || {}
            this.queryParams.post_id.value = row.id
            this.initIndex()
        },
        //打开编辑表单
        openEdit () {
            this.editOpen = true
            this.$set(this, 'findFormData', {
                post_id: this.postData.id,
                user_id: this.postData.user_id,
                chapter_no: this.maxChapterNo + 1
            })
        },
        onBeforeClose() {
            this.$set(this, 'findFormData', {})
            this.beforeClose()
        }
    }
}
</script>

<style scoped>

</style>
